<!DOCTYPE html>
<html>

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>广告管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>
	<link rel="stylesheet" type="text/css" href="/js/bootstrap-fileinput/css/fileinput.min.css">
	<script src="/js/bootstrap-fileinput/js/fileinput.js"></script>
	<script src="/js/bootstrap-fileinput/js/locales/zh.js"></script>
    
</head>

<body class="hold-transition skin-red sidebar-mini">
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">广告管理</h3>
                    </div>

                    <div class="box-body">

                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" onclick="getcontent()"><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" onclick="deleteContent()"><i class="fa fa-trash-o"></i> 删除</button>
                                        <button type="button" class="btn btn-default" title="开启" onclick='confirm("你确认要开启吗？")'><i class="fa fa-check"></i> 开启</button>
                                        <button type="button" class="btn btn-default" title="屏蔽" onclick='confirm("你确认要屏蔽吗？")'><i class="fa fa-ban"></i> 屏蔽</button>
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                                  
                                </div>
                            </div>
                            <!--工具栏/-->

			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">广告ID</th>
									      <th class="sorting">分类ID</th>
									      <th class="sorting">标题</th>
									      <th class="sorting">URL</th>		
									      <th class="sorting">图片</th>	
									      <th class="sorting">排序</th>		
									      <th class="sorting">是否有效</th>											     						      							
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="table">
			                          <!--<tr>
			                              <td><input  type="checkbox"></td>			                              
				                          <td>1</td>
									      <td>1</td>
									      <td>促销海报1</td>
									      <td>http://wwww.hb.com/hd1.html</td>
									      <td>
									      	<img alt="" src="" width="100px" height="50px">
									      </td>
									      <td>1</td>
									      <td>有效</td>									     								     
		                                  <td class="text-center">                                           
		                                 	  <button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal">修改</button>                                           
		                                  </td>
			                          </tr>-->
			                      </tbody>
			                  </table>
			                  <!--数据列表/--> 
                        </div>
                        <!-- 数据表格 /-->
                     </div>
                    <!-- /.box-body -->

		
<!-- 增加编辑窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">广告编辑</h3>
		</div>
		<div class="modal-body">							

			<table class="table table-bordered table-striped"  width="800px">
				<form id="tbcontent">
				<tr>
		      		<td>广告分类</td>
		      		<td>
		      			<select class="form-control" name="categoryId" id="opsion">
		                </select>
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>标题</td>
		      		<td><input  class="form-control"name="title" placeholder="标题" >  </td>
		      	</tr>
			    <tr>
		      		<td>URL</td>
		      		<td><input  class="form-control" placeholder="URL" name="url">  </td>
		      	</tr>	
		      	<tr>
		      		<td>排序</td>
		      		<td><input  class="form-control" name="sortOrder" placeholder="排序" >  </td>
		      	</tr>			      	
		      	<tr>
		      		<td>广告图片</td>
		      		<td>
						<table>
							<tr>
								<td>
									<input type="hidden" name="pic" id="addImg">
									<input type="file" name="myFile" id="myFile" multiple>
					            </td>
							</tr>						
						</table>
		      		</td>
		      	</tr>	      
		      	<tr>
		      		<td>是否有效</td>
		      		<td>
		      		   <input type="checkbox" id="status" class="icheckbox_square-blue" >
		      		</td>
		      	</tr>
				</form>
			 </table>
		</div>
		<div class="modal-footer">
			<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="toadd()">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>

<!--修改窗口-->
  <div class="modal fade" id="updateModel" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="myModalLabels">广告编辑</h3>
			  </div>
			  <div class="modal-body">

				  <table class="table table-bordered table-striped"  width="800px">
					  <form id="updateForm">
						  <tr>
							  <input type="hidden" name="id" id="conId">
							  <td>广告分类</td>
							  <td>
								  <select class="form-control" id="updateSelect" name="categoryId">
								  </select>
							  </td>
						  </tr>
						  <tr>
							  <td>标题</td>
							  <td><input  class="form-control" placeholder="标题" id="title" name="title">  </td>
						  </tr>
						  <tr>
							  <td>URL</td>
							  <td><input  class="form-control" placeholder="URL" id="url" name="url" >  </td>
						  </tr>
						  <tr>
							  <td>排序</td>
							  <td><input  class="form-control" placeholder="排序" id="sortOrder"  name="sortOrder">  </td>
						  </tr>
						  <tr>
							  <td>广告图片</td>
							  <td>
								  <input type="hidden"  id="img" name="pic" width="100px" height="100px">
								  <img src="#" id="imgsrc" width="100px" height="50px">
								  <input type="file" name="myFile" id="imgFile" multiple>

							  </td>
						  </tr>
						  <tr>
							  <td>是否有效</td>
							  <td>
								  <input type="checkbox" id="group" class="icheckbox_square-blue" >
							  </td>
						  </tr>
					  </form>
				  </table>

			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" data-dismiss="modal" aria-hidden="true"  onclick="updateContent()">保存</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>

</body>

</html>
<script>
	$(function () {
		getdata();
		$('#myFile').fileinput({
			language: 'zh', //设置语言
			uploadUrl: "/imgController2/addImg2", //上传的地址
			allowedFileExtensions : ['jpg', 'png','gif'],//接收的文件后缀,
			maxFileCount: 100,
			enctype: 'multipart/form-data',
			showUpload: true, //是否显示上传按钮
			showCaption: false,//是否显示标题
			browseClass: "btn btn-primary", //按钮样式
			previewFileIcon: "<i class='glyphicon glyphicon-king'></i>",
			msgFilesTooMany: "选择上传的文件数量({n}) 超过允许的最大数值{m}！",
		}).on('fileuploaded',function (event,data,previewId,index){
			if(data.response != null){
				if(data.response.code == 100){
					$("#addImg").val(data.response.data);
				}

				if(data.response.code == 101){
					alert(data.response.data);
				}

			}
		})
	})

	function getdata() {
		$.ajax({
			url:"/TbContentController/selectBys",
			type:"post",
			dataType:"json",
			async:false,
			success:function (list) {
				console.log(list)
				var str = list.data;
				var tab = "";
				for(var i = 0; i < str.length; i++){
					tab += '<tr>'
					tab += '<td><input name="boxids" type="checkbox" value="'+str[i].id+'"></td>'
					tab += '<td>'+str[i].id+'</td>'
					tab += '<td>'+str[i].categoryId+'</td>'
					tab += '<td>'+str[i].title+'</td>'
					tab += '<td>'+str[i].url+'</td>'
					tab += '<td>'
					tab += '<img alt="" src="'+str[i].pic+'" width="100px" height="50px">'
					tab += '</td>'
					tab += '<td>'+str[i].sortOrder+'</td>'
					var status;
					if(str[i].status == 1){
						status = "有效";
					}else if(str[i].status == 0){
						status = "无效";
					}
					tab += '<td>'+status+'</td>'
					tab += '<td class="text-center">'
					tab += '<button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#updateModel" onclick="toupdata('+str[i].id+')">修改</button>'
					tab += '</td>'
					tab += '</tr>'
				}
				$("#table").html(tab);
			},
			error:function () {
				alert("代码错误")
			}
		})
	}
	function getcontent() {
		$.ajax({
			url:"/TbContentCategoryController/getContentCatgory",
			type:"post",
			dataType:"json",
			async:false,
			success:function (list) {
				console.log(list)
				var str = list.data;
				var option = "";
				for (var i = 0;i < str.length; i++){
					option += '<option value="'+str[i].id+'">'
					option += str[i].name;
					option += '</option >'
				}
				$("#opsion").html(option);
			},
			error:function () {
				alert("代码错误")
			}
		})
	}

	/*增加*/
	function toadd() {
		var strl;
		if($("#status").prop("checked")){
			strl = "1";
		}else {
			strl = "0";
		}
		$.ajax({
			url:"/TbContentController/toadd",
			type:"post",
			data:$("#tbcontent").serialize()+"&status="+strl,
			dataType:"json",
			async:false,
			success:function (list) {
				console.log(list)

			},
			error:function () {
				alert("代码错误")
			}
		})
	}
	/*删除*/
	function deleteContent(){
		var ids=[];
		$("[name=boxids]:checked").each(function (i,e){
			ids.push($(e).val());
		})
		if(ids.length <= 0){
			alert("请选择要删除的数据");
			return ;
		}
		$.ajax({
			url:"/TbContentController/deleteContent",
			type:"post",
			data:{ids:ids},
			dataType:"json",
			success:function (result){
				if(result.code == 100) {
					getdata();
				}
			},
			error:function (){
				alert("删除失败");
			}
		})
	}

	/*huixian*/
	var imgUrl = '';
	function toupdata(id) {
		alert(id)
		$.ajax({
			url:"/TbContentController/selectByid?id="+id,
			type:"post",
			dataType:"json",
			success:function (result){
				if(result.code == 100){
					var content = result.data.content;
					$("#conId").val(content.id);
					$("#title").val(content.title);
					$("#url").val(content.url);
					$("#imgsrc").prop("src",content.pic);
					imgUrl = content.pic;
					$("#img").val(content.pic);
					$("#sortOrder").val(content.sortOrder);
					if(content.status == 0){
						$("#group").prop("checked",false);
					}else {
						$("#group").prop("checked",true);
					}

					var concateList = result.data.list;
					var selectId = ''
					for(var i = 0; i < concateList.length; i++){
						if(content.categoryId == concateList[i].id){
							selectId += '<option value="'+concateList[i].id+'" selected>'+concateList[i].name+'</option>'
						}else {
							selectId += '<option value="'+concateList[i].id+'">'+concateList[i].name+'</option>'
						}

					}
					$("#updateSelect").html(selectId);
				}
				if(result.code == 101){

				}
			},
			error:function (){
				alert("删除失败");
			}
		})
	}
	$('#imgFile').fileinput({
		language: 'zh', //设置语言
		uploadUrl: "/imgController2/addImg2",//上传的地址
		allowedFileExtensions : ['jpg', 'png','gif'],//接收的文件后缀,
		maxFileCount: 100,
		enctype: 'multipart/form-data',
		showUpload: true, //是否显示上传按钮
		showCaption: false,//是否显示标题
		browseClass: "btn btn-primary", //按钮样式
		previewFileIcon: "<i class='glyphicon glyphicon-king'></i>",
		msgFilesTooMany: "选择上传的文件数量({n}) 超过允许的最大数值{m}！",
		initialPreviewAsData: true,//是否初始预览图片
		initialPreviewFileType: 'image',//回显文件类型(初始预览文件类型)
		initialPreview:"/imgController2/downLoad?filename=" + imgUrl +"",//图片回显(初始预览图片地址)
	}).on('fileuploaded',function (event,data,previewId,index){
		if(data.response != null){
			if(data.response.code == 100){
				$("#img").val(data.response.data);
			}

			if(data.response.code == 101){
				alert(data.response.data);
			}

		}
	})

	function updateContent(){
		var status;
		if($("#group").prop("checked")){
			status = "1";
		}else {
			status = "0";
		}
		$.ajax({
			url:"/TbContentController/updateContent",
			type:"post",
			data:$("#updateForm").serialize() +"&status=" + status,
			dataType:"json",
			success:function (result){
				if(result.code == 100){
					getdata();
				}
				if(result.code == 101){
					alert(result.message);
				}
			},
			error:function (){
				alert("修改失败");
			}
		})
	}

</script>